package com.haoziqi.chapter_06;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
 * description
 * created by A on 2021/3/5
 */
public class Flink02_PV {
    public static void main(String[] args) {
        //1.创建环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment().setParallelism(1);
        SingleOutputStreamOperator<UserBehavior> userBehaviorDS = env.readTextFile("input/UserBehavior.csv")
                .map(new MapFunction<String, UserBehavior>() {
                    @Override
                    public UserBehavior map(String value) throws Exception {
                        String[] datas = value.split(",");
                        return new UserBehavior(
                                Long.valueOf(datas[0]),
                                Long.valueOf(datas[1]),
                                Integer.valueOf(datas[2]),
                                datas[3],
                                Long.valueOf(datas[4])
                        );
                    }
                });
        //过滤出pv数据
        SingleOutputStreamOperator<UserBehavior> pvDS = userBehaviorDS.filter(datas -> "pv".equals(datas.getBehavior()));
       /* //按照wordcount思路
        pvDS.map(new MapFunction<UserBehavior, Tuple2<String,Integer>>() {
            @Override
            public Tuple2<String, Integer> map(UserBehavior value) throws Exception {
                return Tuple2.of("pv",1);
            }
        }).keyBy(data -> data.f0).sum(1).print();*/
       //按照process思路来做
        /*
            为什么不用keyby分组?使用KeyBy分组，不设置分区时，只有一个slot在使用，剩下的slot在空转
         */
        pvDS.process(new ProcessFunction<UserBehavior, Tuple2<String,Integer>>() {
                int count =0;
            @Override
            public void processElement(UserBehavior value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                count++;
               out.collect(Tuple2.of("pv",count));
            }
        }).print();
        //提交任务
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
